package list.options;

import java.util.ArrayList;
import dataStructure.DataCore;
import dataStructure.DataLayerObject;
import ErrorHandling.ErrorDialog;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ListOptionsActivity extends Activity 
{
    /** Called when the activity is first created. */
	
	private DataCore dc;
	private Button b;
	private CheckBox [] CheckBoxes;
	
	ArrayList<String> arrayCoords = new ArrayList<String>();
	ArrayList<String> arrayNames = new ArrayList<String>();
	ArrayList<String> arrayParentNames = new ArrayList<String>();
			
	//public XmlParsing test;
	@Override	
    public void onCreate(Bundle savedInstanceState) 
	{		
        super.onCreate(savedInstanceState);
                
        // Instantiate new DataCore object. 
        dc = new DataCore();
        
        // Set Menu view
        this.CreateMenu();
        
        /* Instantiating a new Button object and adding an OnClickListener to it. */
        b.setOnClickListener(new OnClickListener() 
        {
        	/* Function to handle the onClick of the 'Zoek' button. */
			public void onClick(View arg0) 
			{
				try 
				{					
					// Creating a new intent object 
					Intent intent = new Intent(ListOptionsActivity.this, ShowMapsActivity.class);
												
					//check filters checked
					CheckFilters();
					
					//fill bundles
					fillBundles();
					
					// Creating a new Bundle to hold data that will be sent to the new activity 
					Bundle bundle = new Bundle();
					
					bundle.putStringArrayList("Coordinates", arrayCoords);
					bundle.putStringArrayList("Name", arrayNames);
					bundle.putStringArrayList("ParentName", arrayParentNames);
					
					intent.putExtras(bundle);
						
					// Starting new activity (page 2) 
					ListOptionsActivity.this.startActivity(intent);					
				}
				catch(Exception ex) { Log.i("error", ex.getMessage()); }
			}  
        });       
    }
	
	public void CreateMenu()
	{
		 //create new dynamic view
        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);
        
        //get all level 2 titles dynamic
        ArrayList <DataLayerObject> AllCats = dc.getCats();
        CheckBoxes = new CheckBox[AllCats.size()];
           	         
        for(int i = 0; i < AllCats.size(); i++) {
        	
             CheckBoxes[i] = new CheckBox(this);
             CheckBoxes[i].setText(AllCats.get(i).getTitle());
             ll.addView(CheckBoxes[i]);
        }
        
        //create zoek button
        b = new Button(this);
        b.setText("Zoek");
        ll.addView(b);
        
        //set view
        this.setContentView(sv);        
	}
	
	public void CheckFilters()
	{
		ArrayList <DataLayerObject> AllCats = dc.getCats();		
		
		for(int i = 0; i<dc.getCats().size(); i++)
		{
			dc.getCats().get(i).setVisible(false);
		}
		for(int i = 0; i<AllCats.size(); i++)
		{
			if(CheckBoxes[i].isChecked())			    		    
				AllCats.get(i).setVisible(true);	
			else
				AllCats.get(i).setVisible(false);
				
		}		
	}
	
	 private void fillBundles() 
	 {			
		ArrayList<DataLayerObject> AllVisObjs = dc.getVisibleLayerObjects();
		
		for(int i = 0; i < AllVisObjs.size(); i++) 
		{
			DataLayerObject obj = ((DataLayerObject)AllVisObjs.get(i));
			arrayCoords.add(getChildCoordinates(obj));
			arrayNames.add(obj.getTitle());
			arrayParentNames.add(((DataLayerObject)obj.getParentObject()).getTitle());
		}		
	}

	private String getChildCoordinates(DataLayerObject layerObject) 
	{
		String data = "";
		
		data = ((DataLayerObject)layerObject).getNorth().toString() +
			    "|" + ((DataLayerObject)layerObject).getSouth().toString() +
				"|" + ((DataLayerObject)layerObject).getEast().toString() +
				"|" + ((DataLayerObject)layerObject).getWest().toString();
		
		Log.w("test", data); // testing to see what data goes into the array.
			
		return data;
	}
	
}